package com.favorite;

public class Solution9_79 {
    public boolean exist(char[][] board, String word) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == word.charAt(0)) {
                    if (dfs(board, i, j, word, 0)) return true;
                }
            }
        }
        return false;
    }

    public boolean dfs(char[][] board, int i, int j, String word, int index) {
        if (index == word.length()) return true;
        if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) return false;
        if (board[i][j] != word.charAt(index)) return false;
        char tmp = board[i][j];
        board[i][j] = '0';
        boolean res =
                dfs(board, i - 1, j, word, index + 1) ||
                        dfs(board, i + 1, j, word, index + 1) ||
                        dfs(board, i, j - 1, word, index + 1) ||
                        dfs(board, i, j + 1, word, index + 1);
        board[i][j] = tmp;
        return res;
    }
}

